package com.woniuxy.microsystemsarserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microsystemsarserver.bean.PageRequestBean;
import com.woniuxy.microsystemsarserver.bean.StoreBean;
import com.woniuxy.microsystemsarserver.bean.StoreUserBean;
import com.woniuxy.microsystemsarserver.mapper.StoreMapper;
import com.woniuxy.microsystemsarserver.service.IStoreService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 门店管理业务层实现累
 * @author 杨灿
 */
@Service
@Transactional(readOnly = true)
public class StoreServiceImpl implements IStoreService {
    @Resource
    private StoreMapper storeMapper;
    @Override
    public PageInfo<StoreBean> findByPage(StoreBean storeBean, PageRequestBean pageRequestBean, Long userId) {
        return PageHelper.startPage(pageRequestBean.getPageNum(),pageRequestBean.getLimit()).
                doSelectPageInfo(()->storeMapper.findByPage(storeBean,pageRequestBean,userId));
    }

    @Override
    public List<StoreBean> findAll(Long userId) {

        return storeMapper.findAll(userId);
    }

    @Override
    public StoreBean findById(Long storeId) {

        return storeMapper.findById(storeId);
    }
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public int updateStore(StoreBean storeBean) {

        return storeMapper.updateStore(storeBean);
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public int saveStore(StoreBean storeBean, Long userId) {
        int row = storeMapper.saveStore(storeBean);
        if(row>0){
            StoreUserBean storeUserBean = new StoreUserBean();
            storeUserBean.setFkStoreId(storeBean.getId());
            storeUserBean.setFkUserId(userId);
            int i = storeMapper.saveStoreUser(storeUserBean);
            if(i>0){
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int countByUserId(Long userId) {

        return storeMapper.countByUserId(userId);
    }
}
